#include "Pattern.h"
/*
#include <iostream>
using namespace std;
*/

Pattern::Pattern()
{
	pattern[0][0] = GRASS;
	pattern[1][0] = GRASS;
	pattern[2][0] = GRASS;
	pattern[3][0] = WATER;
	
	pattern[0][1] = GRASS;
	pattern[1][1] = DIRT;
	pattern[2][1] = DIRT;
	pattern[3][1] = WATER;
	
	pattern[0][2] = GRASS;
	pattern[1][2] = GRASS;
	pattern[2][2] = STONE;
	pattern[3][2] = WATER;
	
	colors[GRASS][RED] = 0;
	colors[GRASS][GREEN] = 0.5;
	colors[GRASS][BLUE] = 0;
	
	colors[WATER][RED] = 0;
	colors[WATER][GREEN] = 0;
	colors[WATER][BLUE] = 0.8;
	
	colors[DIRT][RED] = 1;
	colors[DIRT][GREEN] = 0.5;
	colors[DIRT][BLUE] = 0;
	
	colors[STONE][RED] = 0.8;
	colors[STONE][GREEN] = 0.8;
	colors[STONE][BLUE] = 0.8;
	
	characterPosX = 2;
	characterPosZ = 2;
	completed = false;
}

Pattern::~Pattern()
{
}

bool Pattern::checkPattern(CubePileGrid * cubePiles, HouseEmitter * houseEmitter, TextureLoader * textureLoader, int xStart, int zStart)
{
	if(!checkRange(xStart, zStart))
	{
//		cout << " failed range test\n";
		return false;
	}
	
	int height = cubePiles->at(xStart,zStart)->size();
	
	for(int i=xStart; i<xStart + PATTERN_WIDTH; i++)
	{
		for(int j=zStart; j<zStart + PATTERN_HEIGHT; j++)
		{
			CubePile * pile = cubePiles->at(i,j);
			if (pile->isBouncing()
				|| pile->size() != height 
				|| pile->peekAtTop()->blockType != pattern[i-xStart][j-zStart])
			{
				/*
				cout << "Failed tests at x:"<<i << " y:" << j;
				if(pile->isBouncing())
				{
					cout  <<" still bouncing";
				}
				else if (pile->size() != height)
				{
					cout << " size mismatch, should be: " << height << " but is: " << pile->size(); 
				}
				else if (pile->peekAtTop()->blockType != pattern[i-xStart][j-zStart])
				{
					cout << " wrong block, at pattern pos: "<< i-xStart << " " << j-zStart << " type is: " << pattern[i-xStart][j-zStart] << " but actual block is type: " << pile->peekAtTop()->blockType;
				}
				else
				{
					cout << " unknown reason... WTF?";
				}
				cout << "\n";
				*/
				return false;
			}
		}
	}
	
	for(int i=xStart; i<xStart + PATTERN_WIDTH; i++)
	{
		for(int j=zStart; j<zStart + PATTERN_HEIGHT; j++)
		{
			cubePiles->at(i,j)->lock();
		}
	}
	
	houseEmitter->add(new RoofCorner(textureLoader, xStart+1, zStart+1, ROOF_LEFT));
	houseEmitter->add(new BrownBlock(textureLoader, xStart+1, zStart + 1));
	houseEmitter->add(new Window(textureLoader, xStart+1, zStart+1));
	
	
	houseEmitter->add(new RoofCorner(textureLoader, xStart+2, zStart + 1, ROOF_RIGHT));
	houseEmitter->add(new Door(textureLoader, xStart+2, zStart+1));
	
	solvedAtLeft = xStart;
	solvedAtTop = zStart;
	
	completed = true;
	
	return true;
}

void doReward(CubePile * cubePiles[GRID_SIZE][GRID_SIZE], int left, int top)
{
	for(int i=left; i<PATTERN_WIDTH; i++)
	{
		for(int j=top; j<PATTERN_HEIGHT; j++)
		{
			cubePiles[i][j]->lock();
		}
	}
}

bool Pattern::checkRange(int xStart, int yStart)
{
//	cout << "checking range at:"<<xStart << " " << yStart << "\n";
	if(xStart + PATTERN_WIDTH > GRID_SIZE)
	{
//		cout << "x too high: " << xStart << "\n";
		return false;
	}
	if (yStart + PATTERN_HEIGHT > GRID_SIZE)
	{
//		cout << " y too high: " << yStart << "\n";
		return false;
	}
	return true;  
}

void Pattern::render()
{
	glPushMatrix();
		glBegin(GL_QUADS);
			for(int i=0; i<PATTERN_WIDTH; i++)
			{
				for(int j=0; j<PATTERN_HEIGHT; j++)
				{
					int color = pattern[i][j];
					glColor4f(colors[color][RED], colors[color][GREEN], colors[color][BLUE], 1);
					glVertex3f(i, -(j+0.9), 0);
					glVertex3f(i+0.9, -(j+0.9), 0);
					glVertex3f(i+0.9, -j, 0);
					glVertex3f(i, -j, 0);
				}	
			}
		glEnd();
	glPopMatrix();
}

int Pattern::getCharLeft()
{
	return characterPosX + solvedAtLeft;
}

int Pattern::getCharTop()
{
	return characterPosZ + solvedAtTop;
}
